/**
 * 
 */
package com.xth8013.as.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.xth8013.as.domain.User;
import com.xth8013.as.domain.UserConfigParam;
import com.xth8013.as.service.UserService;
import com.xth8013.as.util.ObjectUtils;

/**
 * 
 * @Description:用户身份验证拦截器
 * @author: tianhua.xie
 * @Date: 2017年2月9日 上午12:47:39
 *
 */
@Component
public class AuthenticationInterceptor extends HandlerInterceptorAdapter {
	private UserService userService;

	protected org.springframework.web.context.WebApplicationContext wac; // WEB程序上下文环境
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {

		String requestUri = request.getRequestURI();
		String contextPath = request.getContextPath();
		String url = requestUri.substring(contextPath.length());
		// requestUri:/as/menuMgrController/menuMgr
		// contextPath:/as
		// url:/menuMgrController/menuMgr
		System.out.println("requestUri:" + requestUri);
		// System.out.println("contextPath:" + contextPath);
		// System.out.println("url:" + url);

		// 获取请求类型
		String type = request.getHeader("X-Requested-With") == null ? "" : request.getHeader("X-Requested-With");// XMLHttpRequest

		// System.out.println("type = " + type);//type = XMLHttpRequest
		User loginedUser = (User) request.getSession().getAttribute("loginedUser");
		if (ObjectUtils.isBlank(loginedUser)) {
			// 获取登录的用户信息，并判空
			wac = org.springframework.web.context.support.WebApplicationContextUtils
					.getWebApplicationContext(request.getServletContext());
			userService = (UserService) wac.getBean("userService"); // spring的组件都行，包含@Component，@Service，@Repository等等

			String loginedUserAccount = (String) request.getSession().getAttribute("loginedUserAccount");
			loginedUser = userService.getUserByAccount(loginedUserAccount);
			System.out.println(loginedUserAccount);
			request.getSession().setAttribute("loginedUser", loginedUser);


		}

		if (ObjectUtils.isNotBlank(loginedUser)) {
			UserConfigParam param = userService.loadUserConfigParamById(loginedUser.getUserId());
			request.getSession().setAttribute("configParam", param);
			System.out.println(param);
		}
		if (ObjectUtils.isBlank(loginedUser)) {
			if (type.equals("XMLHttpRequest")) {
				// 处理ajax请求
				String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
						+ contextPath + "/";
				// basePath = http://localhost:8081/as/
				// System.out.println("basePath = " + basePath);

				response.setHeader("SESSIONSTATUS", "TIMEOUT");
				response.setHeader("CONTEXTPATH", basePath + "securit yController/toL98J7yuogin");
				response.setStatus(HttpServletResponse.SC_OK);

				return false;
			} else {
				response.sendRedirect(contextPath + "/securityController/toLogin");
				return false;
			}
		} else
			return true;

	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}
}
